package tt.com.carmanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.carmanager.service.FuneralDriverTaskService;
import tt.com.carmanager.vo.DriveTaskGoodVo;
import tt.com.carmanager.vo.TeamTimeVo;
import tt.com.common.enums.Status;
import tt.com.common.model.DriveTask;
import tt.com.common.util.CommonResult;
import tt.com.common.util.PageUtil;

import java.util.List;

/**
 * @author BEAN
 * @version 2020
 * @date 2022/8/25 0:18
 */
@RestController
@RequestMapping("/car/task")
public class FuneralDriverTaskController {

    @Autowired
    private FuneralDriverTaskService funeralDriverTaskService;

    /**
     * 根据账号判断权限  再输出不同的任务列表信息
     * @param userName 账号
     * @return 任务列表信息
     */
    @PostMapping("/show/task/{userName}")
    public CommonResult showTaskListByUserName(@PathVariable String userName){
        List<DriveTask> driveTasks = funeralDriverTaskService.showTaskByUserName(userName);
        if (driveTasks == null){
            return CommonResult.buildR(Status.NO_FIND);
        }
        return CommonResult.buildR(Status.OK, driveTasks);
    }


    /**
     * 分页显示任务列表信息
     * @return 分页后的任务列表信息 默认每页10条
     */
    @RequestMapping("/show/page")
    public CommonResult showTaskByPage(@RequestBody PageUtil pageUtil){
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null){
            return CommonResult.buildR(Status.PARAM_ISNULL);
        }
        pageUtil = funeralDriverTaskService.showTaskByPage(pageUtil);
        return CommonResult.buildR(Status.OK, pageUtil);
    }


    /**
     * 逻辑删除 （更新data_status状态）
     * @param driveTask
     * @return
     */
    @PostMapping("/show/deleById")
    public CommonResult showTaskDelById(@RequestBody DriveTask driveTask){
        Boolean result = funeralDriverTaskService.showTaskDeleteById(driveTask.getId(), driveTask.getDeleteUserId());
        if (result){
            return CommonResult.buildR(Status.OK, "删除成功！");
        }
        return CommonResult.buildR(Status.DEFAULT,"任务不存在或已删除！");
    }

    /**
     * 选择出车时间
     * @param driveTask
     * @return
     */
    @PostMapping("/show/updateStart")
    public CommonResult showTaskUpdateStart(@RequestBody DriveTask driveTask){
        Boolean result = funeralDriverTaskService.showTaskChooseStart(driveTask);
        if (result){
            return CommonResult.buildR(Status.OK, "出车时间选择成功！");
        }
        return CommonResult.buildR(Status.SYSTEM_ERROR);
    }

    /**
     * 选择回车时间
     * @param driveTask
     * @return
     */
    @PostMapping("/show/updateReturn")
    public CommonResult showTaskUpdateReturn(@RequestBody DriveTask driveTask){
        Boolean result = funeralDriverTaskService.showTaskChooseStart(driveTask);
        if (result){
            return CommonResult.buildR(Status.OK, "回车时间选择成功！");
        }
        return CommonResult.buildR(Status.SYSTEM_ERROR);
    }

    /**
     * 出车物品选择  countCostByTask
     * @param vo 出车物品、出车任务id的封装类
     * @return commonResult
     */
    @RequestMapping("/show/choose")
    public CommonResult chooseGoods(@RequestBody DriveTaskGoodVo vo){
        CommonResult commonResult = funeralDriverTaskService.chooseDriveGoods(vo.getWarehouseGoods(), vo.getTaskId());
        return commonResult;
    }

    /**
     * 统计班组在时间区间内的费用
     * @param teamTimeVo 开始时间 结束时间 班组id封装vo类
     * @return 查询结果
     */
    @PostMapping("show/countByTeam")
    public CommonResult countCostByTeamId(@RequestBody TeamTimeVo teamTimeVo){
        CommonResult commonResult = funeralDriverTaskService.countCostByTeamId(teamTimeVo);
        return commonResult;
    }
}
